<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
//$this->model('task')->test();
$return['status']=$this->member_status;
$tabs=array(
	array('op'=>'list','title'=>'全部用户'),
); 
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['jsjiqi'])){
		$params['jsjiqi']=intval($_RQ['jsjiqi']);
		$return['params']['jsjiqi']=intval($_RQ['jsjiqi']);
	}
	if($_RQ['mid']){
		$params['mid']=$_RQ['mid'];
		$return['params']['mid']=$_RQ['mid'];
	}
	if($_RQ['level_id']){
		$params['level_id']=$_RQ['level_id'];
		$return['params']['level_id']=$_RQ['level_id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['psize']){
		$_SESSION['psize']=$_RQ['psize'];
	}
	$psize = $_SESSION['psize']?:10;
	$list=pdo_getslice('jy_mjicai_member',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		foreach($list as $k=>&$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$credit=pdo_get('core_members',array('id'=>$item['mid']),array('from_os','credit2','credit3','credit3_fz'));
			$item['from_os']=get_ostitle()[$credit['from_os']];
			$item['jifen']=pdo_getsum('jy_mjicai_feelog',array('merchid'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid'],'type'=>array(0,1,3,5,6,9,21)),'fee');
			$mids=pdo_getall('jy_mjicai_member',array('mid'=>$item['mid']),'fromuser','fromuser');
			if($mids){
				$mids=array_keys($mids);
			}
			$mids[0]=$item['mid'];
			$item['salefee']=pdo_getsum('jy_mjicai_agent_order',array('mid'=>$mids),'order_price');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：</br>这里只显示本金豆理财的会员！','edit'=>false,'del_one'=>false,'delall'=>false,
		'total'=>$total,
		'psize'=>$psize,
		'list'=>array(
			array('name'=>'mid','title'=>'会员Mid','type'=>'string'),
			array('name'=>'merchid','title'=>'所属商户','type'=>'select','options'=>'merchs','option_title'=>'merchname'),
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'from_os','title'=>'来源','type'=>'string'),
			//array('name'=>'level_id','title'=>'会员等级','type'=>'select','options'=>'levels'),
			array('name'=>'salefee','title'=>'销售额','type'=>'string'),
			array('name'=>'jifen','title'=>'获得金豆','type'=>'string'),
			//array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		)),
		'search'=>array(
			//array('name'=>'level_id','width'=>15,'title'=>'分销等级','type'=>'select','options'=>'levels'),
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),

		)
	);
	$return['merchs']=pdo_getall('jy_merch_user',array('uniacid'=>$_SESSION['uniacid']),array('id','merchname'),'id');
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_mjicai_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array('get_type'=>0);
	}
	$item['jiqiendtime']=date('Y-m-d H:i:s',$item['jiqiendtime']);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'edit'=>array(
				array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['jsjiqi']=array('未开通','已开通');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'jsjiqi'=>intval($postdata['jsjiqi']),
		'jiqiendtime'=>strtotime($postdata['jiqiendtime']),
		'status'=>intval($postdata['status']),
		'freeze'=>$postdata['freeze'],
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_mjicai_member',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_mjicai_member',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='chongzhi'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(intval($post['money'])<=0){
			exi('请先设置充值金额！并且为整数！','error');
		}
		$fee=sprintf("%.2f", intval($post['money']));
		$this->model('member')->recharge($post['mid'],$fee,0,'充值'.$this->db_title);
		exi('充值'.$this->db_title.'成功！','','list');
	}
	$item=$this->get('member',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定会员不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'手动充值'.$this->db_title,
		'edit'=>array(
			array('name'=>'tip','title'=>'<red>请谨慎操作</red>，后台手动充值'.$this->db_title.'，消息记录会产生充值记录!','type'=>'tip'),
			array('name'=>'money','title'=>'要充值的'.$this->db_title.'金额','type'=>'string'),
		),
	);
	exi($return);
}
if($_RQ['op']=='jiedong'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if($post['fee']>$post['freeze']){
			exi('要解冻的金额不能大于当前冻结！','error');
		}
		$this->model('member')->recharge($post['mid'],$post['fee'],3,'手动解冻');
		exi('解冻成功！','','list');
	}
	$item=$this->get('member',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定分销不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'手动解冻',
		'edit'=>array(
			array('name'=>'tip','title'=>'<red>请谨慎操作</red>，只有出现异常,没办法时才可操作!','type'=>'tip'),
			array('name'=>'freeze','title'=>'当前冻结','type'=>'copy'),
			array('name'=>'fee','title'=>'要解冻的金额','type'=>'string'),
		),
	);
	exi($return);
}